//
//  CallRecrodCell.m
//  NetPhone
//
//  Created by intel intel on 12-4-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CallRecrodCell.h"

@implementation CallRecrodCell
@synthesize callRecord;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        nameLabel = [[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 180, 45)]autorelease];
        nameLabel.font = [UIFont boldSystemFontOfSize:18];
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:nameLabel];

        phoneLabel = [[[UILabel alloc]initWithFrame:CGRectMake(10, 25, 180, 20)]autorelease];
        phoneLabel.font = [UIFont systemFontOfSize:14];
        phoneLabel.textColor = [UIColor grayColor];
        phoneLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:phoneLabel];

        countLabel = [[[UILabel alloc]initWithFrame:CGRectZero]autorelease];
        countLabel.font = [UIFont systemFontOfSize:18];
        countLabel.textColor = [UIColor grayColor];
        countLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:countLabel];
        
        if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {
            timeLabel=[[[UILabel alloc]initWithFrame:CGRectMake(180, 0, 80, 45)]autorelease];
        }else
        {
            timeLabel = [[[UILabel alloc]initWithFrame:CGRectMake(200, 0, 80, 45)]autorelease];
        }
        timeLabel.font = [UIFont systemFontOfSize:14];
        timeLabel.textColor = [UIColor blueColor];
        timeLabel.textAlignment = UITextAlignmentRight;
        timeLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:timeLabel];
        
        
    }
    return self;
}

-(void)setCallRecord:(CallRecord *)_callRecord
{
    if (_callRecord != callRecord) {
        [callRecord release];
        callRecord = [_callRecord retain];
    }
    
    int height;
    if ([callRecord.phoneNum isEqualToString:callRecord.phoneName]) {
        height = 45;
        nameLabel.frame = CGRectMake(10, 0, 180, 45);
        phoneLabel.hidden = YES;
    }else{
        height = 25;
        nameLabel.frame = CGRectMake(10, 0, 180, 25);
        phoneLabel.text = callRecord.phoneNum;
        phoneLabel.hidden = NO;
    }
    
    
    NSString *text = callRecord.phoneName;
    nameLabel.text = text;
    
    if (callRecord.count >1) {
        countLabel.hidden = NO;
        CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:18]];
        countLabel.frame = CGRectMake(size.width +15, 0, 40, height);
        countLabel.text = [NSString stringWithFormat:@"(%d)",callRecord.count];
    }else{
        countLabel.hidden = YES;
    }
    
    NSDateFormatter *datef=[[[NSDateFormatter alloc] init]autorelease];
    [datef setDateFormat:@"yyyy-MM-dd"];
    NSString *theDate = [datef stringFromDate:[NSDate date]];

    if ([theDate isEqualToString:callRecord.callDate]) {
        timeLabel.text = callRecord.callTime;
    }else{
        timeLabel.text = callRecord.callDate;
    }
    
    
}

-(void)dealloc
{
    [callRecord release];
    [super dealloc];
}


@end
